package com.dream.service.user.web.processor.param;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.dream.service.user.domain.model.UserAddrModel;
import com.dream.service.user.domain.model.UserModel;
import com.dream.service.user.web.model.FieldSchema;
import com.dream.service.user.web.processor.exception.ParamException;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kangd on 2016/3/25.
 */
public class ParamProcessor {
    //email telephone  name detail_address1 detail_address2  city state zip IDnumber country isDefault
    public static UserAddrModel processorUserAddr(HttpServletRequest req) throws ParamException {
        String receiving_String = req.getParameter(FieldSchema.USER_ADDR_ID);
        Integer receiving_id=null;
        if(StringUtils.isNotEmpty(receiving_String)){
            receiving_id =Integer.valueOf(receiving_String);
        }
        String pin = req.getParameter(FieldSchema.PIN);
        String email = req.getParameter(FieldSchema.EMAIL);
        String telephone = req.getParameter(FieldSchema.TELEPHONE);
        String name  = req.getParameter(FieldSchema.NAME);
        String detail_address1 = req.getParameter(FieldSchema.DETAIL_ADDR1);
        String detail_address2 = req.getParameter(FieldSchema.DETAIL_ADDR2);
        String city = req.getParameter(FieldSchema.CITY);
        String state = req.getParameter(FieldSchema.STATE);
        String zip = req.getParameter(FieldSchema.ZIP);
        String IDnumber = req.getParameter(FieldSchema.IDNUMBER);
        String country = req.getParameter(FieldSchema.COUNTTY);
        String country_name = req.getParameter(FieldSchema.COUNTTY_NAME);
        String isDefaultString = req.getParameter(FieldSchema.USER_ADDR_ISDEFAULT);
        Integer isDefault=null;
        if(StringUtils.isNotEmpty(isDefaultString)){
            isDefault =Integer.valueOf(isDefaultString);
            if(!isDefault.equals(0) && !isDefault.equals(1)){
                throw  new ParamException("Param Error") ;
            }
        }else{
            throw  new ParamException("Param Error") ;
        }
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(detail_address1)  )
        {
            throw  new ParamException("pin or address is must") ;
        }
        String storeId = req.getParameter("storeid");
        String cityName = req.getParameter("cityName");
        UserAddrModel userAddrModel  = new UserAddrModel(receiving_id, pin, email, telephone, name,detail_address1,detail_address2, city, state, zip, IDnumber, country,isDefault) ;
        userAddrModel.setCityName(cityName);
        userAddrModel.setStoreId(storeId);
        userAddrModel.setCountry(country);
        userAddrModel.setCountry_name(country_name);
        return userAddrModel;
    }

    // uuid pin token nickname icon
    public static UserModel processorUser(String pin, HttpServletRequest req) throws ParamException {
        String icon = req.getParameter(FieldSchema.ICON);
        String nickname = req.getParameter(FieldSchema.NICKNAME);
        String pwd = req.getParameter(FieldSchema.PASSWD);
        if(StringUtils.isEmpty(icon) && StringUtils.isEmpty(nickname) && StringUtils.isEmpty(pwd)){
            throw  new ParamException("Param Error:nickname,icon  or pwd") ;
        }
        UserModel userModel =  new UserModel(pin, nickname, icon, null, pwd,null);
        return userModel;
    }
}
